﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using Fisharoo.Common;
using Fisharoo.DataAccess;
using Fisharoo.DataAccess.Interfaces;
using Fisharoo.Interfaces;
using Fisharoo.Web.Account.Interfaces;

namespace Fisharoo.Web.Account.Presenters
{
    public class RegisterPresenter
    {
        private IRegister _view;
        [Import]
        private IAccountRepository _accountRepository;
        [Import]
        private IPermissionRepository _permissionRepository;
        [Import]
        private ITermRepository _termRepository;
        [Import]
        private IAccountService _accountService;
        [Import]
        private Fisharoo.Interfaces.IWebContext _webContext;
        [Import]
        private IEmail _email;
        [Import]
        private IConfiguration _configuration;
        [Import]
        IRedirector _redirector;

        public void Init(IRegister View)
        {
            _view = View;
            MEFManager.Compose(this);

            _view.LoadTerms(_termRepository.GetCurrentTerm());
            _view.LoadreCaptchaSetting((bool)_configuration.GetConfigurationSetting(typeof(bool), "reCaptcha"));
        }

        public void LoginLinkClicked()
        {
            _redirector.GoToAccountLoginPage();
        }

        public void Register(string Username, string Password,
            string FirstName, string LastName, string Email,
            string Zip, DateTime BirthDate, bool isCaptchaValid, bool AgreesWithTerms, Int32 TermID)
        {
            if (AgreesWithTerms)
            {
                if (isCaptchaValid)
                {
                    Fisharoo.DataAccess.Account acc = new Fisharoo.DataAccess.Account();
                    acc.FirstName = FirstName;
                    acc.LastName = LastName;
                    acc.Email = Email;
                    acc.BirthDate = BirthDate;
                    acc.Zip = Zip;
                    acc.Username = Username;
                    acc.Password = Password.Encrypt(Password);
                    acc.TermID = TermID;
                    //TODO: For development marked as verified already
                    acc.EmailVerified = true;

                    if (_accountService.EmailInUse(Email))
                    {
                        _view.ShowErrorMessage("This email is already in use!");
                        _view.ToggleWizardIndex(0);
                    }
                    else if (_accountService.UsernameInUse(Username))
                    {
                        _view.ShowErrorMessage("This username is already in use!");
                        _view.ToggleWizardIndex(0);
                    }
                    else
                    {
                        _accountRepository.SaveAccount(acc);

                        List<Permission> permissions = _permissionRepository.GetPermissionByName("PUBLIC");
                        Fisharoo.DataAccess.Account newAccount = _accountRepository.GetAccountByEmail(Email);

                        if (permissions.Count > 0 && newAccount != null)
                        {
                            _accountRepository.AddPermission(newAccount, permissions[0]);
                        }

                        _email.SendEmailAddressVerificationEmail(acc.Username, acc.Email);

                        _view.ShowAccountCreatedPanel();
                    }
                }
                else
                {
                    _view.ShowErrorMessage("Your entry doesn't match the reCAPTCHA image.  Please try again.");
                }
            }
            else
            {
                _view.ToggleWizardIndex(2);
                _view.ShowErrorMessage("You can't create an account on this site if you don't agree with our terms!");
            }
        }
    }
}
